<?php

namespace app\common\model\mall\shop;

use think\Model;

class Cat extends Model
{
    // 表名
    protected $name = 'mall_shop_cat';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'shop_id_text',
        'status_text'
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getStatusList()
    {
        return ['normal' => __('Normal'),'hidden' => __('Hidden')];
    }

    public function getShopIdTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shop_id']) ? $data['shop_id'] : '');
        $name = $this->shopId = \app\common\model\mall\shop\Shop::getFieldByUserId($value,"name");
        return $name?$name:"";
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 判断是否当前页面
     * @param $value
     * @param $data
     * @return bool
     */
    public function getIsActiveAttr($value, $data)
    {
        $url = request()->url();
        $thisV = \think\View::instance();
        $channel = collection($thisV->__NAV__)->toArray();
        if ((!empty($channel) && ($channel[0]['id'] == $data['id'] )) || $data['navUrl'] == $url) {
            return true;
        }
        return false;
    }

    /**
     * 获取导航列表
     * @param $tag
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getCatList($tag)
    {
        $type = empty($tag['type']) ? '' : $tag['type'];
        $typeid = !isset($tag['typeid']) ? '' : $tag['typeid'];
        $condition = empty($tag['condition']) ? '' : $tag['condition'];
        $field = empty($params['field']) ? '*' : $params['field'];
        $row = empty($tag['row']) ? 10 : (int)$tag['row'];
        $orderby = empty($tag['orderby']) ? 'weigh' : $tag['orderby'];
        $orderway = empty($tag['orderway']) ? 'asc' : strtolower($tag['orderway']);
        $limit = empty($tag['limit']) ? $row : $tag['limit'];
        $cache = !isset($tag['cache']) ? true : (int)$tag['cache'];
        $imgwidth = empty($tag['imgwidth']) ? '' : $tag['imgwidth'];
        $imgheight = empty($tag['imgheight']) ? '' : $tag['imgheight'];
        $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'asc';
        $cache = !$cache ? false : $cache;
        $where = [];
        $where['status'] = "normal";
        //$where['pid'] = 0;

        $order = $orderby == 'rand' ? 'rand()' : (in_array($orderby, ['createtime', 'weigh', 'id']) ? "{$orderby} {$orderway}" : "createtime {$orderway}");
        $list = self::where($where)
            ->where($condition)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->cache($cache)
            ->select();
        return $list;
    }

    /**
            * 获取店铺商品分类列表
     */
    public function getShopCats($shop_id = 0){
        $data = [];
        if(!$data){
            $data = $this->field("id,pid,name,shop_id")->where(["shop_id"=>$shop_id,"pid"=>0,"ismenu"=>1 ,"status"=>'normal'])->order("weigh asc")->select();
            if(count($data)>0){
                $ids = array();
                foreach ($data as $v){
                    $ids[] = $v['id'];
                }
                
                $crs = $this->field("id,pid,name,shop_id")
                ->where(["shop_id"=>$shop_id,"ismenu"=>1 ,"status"=>'normal'])
                ->where("pid","in",implode(',',$ids))
                ->order("weigh asc")->select();
                $ids = array();
                foreach ($crs as $v){
                    $ids[$v['pid']][] = $v;
                }
                foreach ($data as $key =>$v){
                    $data[$key]['children'] = '';
                    if(isset($ids[$v['id']])){
                        $data[$key]['children'] = $ids[$v['id']];
                    }
                }
            }
        }
        return $data;
    }
    
    
}
